package com.cenntro.common.websocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * 
* @ClassName: WebsocketChatServerInitializer
* @Description: TODO
* @author Administrator
* @date 2019年7月25日
*
 */
public class WebsocketServerInitializer extends ChannelInitializer<SocketChannel> { // 1

    @Override
    public void initChannel(SocketChannel ch) throws Exception {// 2
        ChannelPipeline pipeline = ch.pipeline();
        
        pipeline.addLast("logging",new LoggingHandler("DEBUG"));//设置log监听器，并且日志级别为debug，方便观察运行流程
        pipeline.addLast("http-codec",new HttpServerCodec());//设置解码器
        pipeline.addLast("aggregator",new HttpObjectAggregator(65536));//聚合器，使用websocket会用到
        pipeline.addLast("http-chunked",new ChunkedWriteHandler());//用于大数据的分区传输

//        pipeline.addLast(new WebSocketServerProtocolHandler("/ws/"));
//        pipeline.addLast(new WebSocketServerProtocolHandler("/ws/",null,true,65535,true));
        pipeline.addLast(new TextWebSocketFrameHandler());
    }
}